/*
 *	jquery.placeholder.js - copyright 2004-2010, GuCong
 *	http://gucong.co.cc/textbox/
 *	http://www.gnu.org/licenses/gpl.html
 */
(function(win, $, undefined){
	
	//请在这里修改placeholder的字体颜色
	var color = "darkGray";
	
	if($("<input>").prop("placeholder") == undefined) {
		function setFocus(input){
			setTimeout( function(){
				$(input).focus();
			}, 2);
		}

		function placeholder(input){
			var text = input.getAttribute("placeholder") || "",
				place = $.data(input, "placeholder"),
				holder;
			if(place){
				holder = place.children();
			}else{
				holder = $("<placeholder>");
				place = holder.clone().mousedown(function(){
					setFocus(input);
				}).append(holder);
				$.data(input, "placeholder", place);

				$(input).bind("blur.placeholder", function(){
					set();
					holder.show();
				}).bind("focus.placeholder", function(){
					holder.hide();
				}).bind("change.placeholder", set);
	
			}
			input = $(input);
			function set(){
				if(text && !input.val()){
					holder.html(text).css({
						paddingLeft: input.css("paddingLeft"),
						marginLeft: input.css("marginLeft"),
						left: input.css("borderLeftWidth"),
						verticalAlign: "center",
						whiteSpace: "nowrap",
						position: "absolute"
					});
					var height = input.css("height")
					place.css({
						fontSize: input.css("fontSize"),
						verticalAlign: "middle",
						display: "inline-block",
						position: "relative",
						lineHeight: height,
						height: height,
						color: color
					});
					input.before(place);
				} else {
					place.hide();
				}
			}
			setTimeout( set, 1);
		}
		
		$.attrHooks.placeholder = $.propHooks.placeholder = {
			set: function(element, value){
				element.setAttribute("placeholder", value);
				placeholder(element);
				return value;
			},
			get: function(element){
				placeholder(element);
				return element.getAttribute("placeholder");
			}
		}

		$(function(){
			$("[placeholder]").each(function(index, element) {
				placeholder(element);
			});
			setFocus("[autofocus]");
		});
	} else {
		color = "placeholder{color:" + color + ";}";
		var style = ":focus::-webkit-input-placeholder{color:transparent !important;}";
		$.each([":-webkit-input-","-webkit-","-moz-",":-ms-",":-o-",""],function(i, name){
			style += ":" + name + color;
		});
		$(document.documentElement).prepend($("<style>").html(style));
	}
})(this, this.jQuery);

(function(win, $, undefined){

	if($("<input>").prop("oninvalid") === undefined){
		var html = win.document.documentElement;

		$(html).bind("submit.invalid", function(e) {
			$(e.target).not("[novalidate]").find("[pattern],[required]").each(function(index, element) {
				var value = this.value,
					input = $(this);
				if(
					value ?
						(function(){
							var regexp = input.attr("pattern");
							if(regexp){
								return !new RegExp(regexp).test(value);
							}
						})()
					:
					input.is("[required]")
				){
					$(this).trigger("invalid");
					e.preventDefault();
					e.stopImmediatePropagation();
				}
			});
			return false;
		});

		var onerror;
		$(html).bind("invalid", function(e) {
			if(onerror) {
				return;
			}
			onerror = true;
			var input = $(e.target).focus(),
				offset = input.offset(),
				msg = $("<div>").html(input.val() ? "请与所要求的格式保持一致" : "请填写此字段").css({
					top: offset.top + input.outerHeight() + 3,
					border: "1px solid #ccc",
					position: "absolute",
					background: "#FFF",
					left : offset.left,
					padding: "10px",
					color: "#333"
				}).appendTo(html);
			function closemsg(){
				msg.remove();
				onerror = false;
			}
			setTimeout(closemsg,6000);
			setTimeout(function(){
				input.one("focus.invalid click.invalid", closemsg);
			},1);
		});
	}

})(this, this.jQuery);
